package com.example.doc.controller;

import com.example.doc.VO.DictVO;
import com.example.doc.model.Dict;
import com.example.doc.model.RespBean;
import com.example.doc.service.DictService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/dict")
public class DictController {

    @Resource
    DictService dictService;

//    @GetMapping
//    public List<Dict> getAllDict(){
//        return dictService.getAllDicts();
//    }

    @GetMapping
    public List<DictVO> getAllDict(){
        return dictService.getAllDicts();
    }

    @PostMapping("/add")
    public RespBean addDict(@RequestBody Dict dict){
        if (dictService.saveDict(dict) == 1){
            return RespBean.ok("添加成功!");
        }
        return RespBean.error("添加失败!");
    }

    @DeleteMapping("/delete/{id}")
    public RespBean deleteDict(@PathVariable Long id){
        if (dictService.deleteDictById(id) == 1) {
            return RespBean.ok("删除成功!");
        }
        return RespBean.error("删除失败!");
    }

    @PutMapping("/edit")
    public RespBean updateDict(@RequestBody Dict dict){
        if (dictService.updateDict(dict) == 1) {
            return RespBean.ok("更新成功!");
        }
        return RespBean.ok("更新失败!");
    }

    @GetMapping("/parent")
    public List<Dict> getParentDict(){
        return dictService.getParentDict();
    }
}
